#!/bin/bash

#by neil.qiao 2016-11-24

#脚本逻辑：（判断逻辑从紧到宽）
#   1、若同时存在.lock/.noeng/.lockeng：说明下载时没有eng版，当前正在下载。退出脚本。
#   2、若只同时存在.lock/.noeng：说明下载时没有eng版，并开始下载。下载开始前，并创建.lockeng文件，防止还未下载完成，在再次自动执行脚本时再下载以出现错误。lftp命令完成后，检查eng版
#      是否存在，存在删除.lock/.noeng及同步前创建的.lockeng则退出，此版本全部同步完成；否则，退出并删除.lockeng，在下次再自动同步。
#   3、若只存在.lock，说明正在下载新版FalconBox。退出脚本。
#   4、若都不存在，则判断北京ftp是否更新了FalconBox，并开始下载新版。下载开始前，创建.lock，防止还未下载完成，在再次自动执行脚本时再下载以出现错误。下载完成后，检查eng版是否存在：
#      存在则删除.lock并退出脚本，此版本完全同步完成；否则，创建.noeng，此时.lock和.noeng同时存在，成为下次执行2的条件。

#定义pullFalconBox函数，传输rom、fullpkg目录及build-info.txt。
function pullFalconBox () {
    BuildDir=`curl -s 'http://10.10.194.14:8848/buildrepo/api/builds/lastbuild?g=falcon&m=FalconBox' | awk -F "\"" '{print $4"/"$8"/"$12}'` 
    NextBuildNumber=`curl -s 'http://10.10.194.14:8848/buildrepo/api/builds/nextbuildnumber?g=falcon&m=FalconBox' | tr -cd "[0-9]"` 
    LocalNextBuildNumber=`/bin/more '/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.buildnumber' | /usr/bin/awk -F "=" '{print $2}'`

    cd "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/"
    if [[ ${NextBuildNumber} -gt ${LocalNextBuildNumber} ]]; then
        if [[ -d ${BuildDir} ]]; then
            echo -e "\n`date +%Y.%m.%d" "%H:%M`  failed！当前版本已存在，请检查问题！" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
            echo "build.number=${NextBuildNumber}" > .buildnumber
            exit 3
        fi
    else
        echo -e "\n`date +%Y.%m.%d" "%H:%M`  当前版本已最新！" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
        exit 4
    fi

    echo ${BuildDir} > .lock
    #进入BuildDir，ftp连接下载.buildnumber、rom目录、full_pkg目录和build-info.txt
    mkdir -p ${BuildDir}
    cd ${BuildDir} 
    #lftp或ftp命令均无法调用shell变量，解决方案：将变量${BuildDir}写入ftp.txt文件中，提供给lftp使用。
    echo "open 10.10.194.14:8021" > ftp.txt
    echo "cd scmbuilds/qa/falcon/FalconBox/" >> ftp.txt
    echo "cd ${BuildDir}" >> ftp.txt
    echo "get build-info.txt" >> ftp.txt
    echo "mirror full_pkg/" >> ftp.txt
    echo "mirror rom/" >> ftp.txt 
    echo "close" >> ftp.txt
    echo "bye" >> ftp.txt

    echo -e "\n`date +%Y.%m.%d" "%H:%M`开始同步${BuildDir}" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
    lftp < ftp.txt
    rm ftp.txt
    echo "build.number=${NextBuildNumber}" > "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.buildnumber"

    cd rom/
    userVersion=`ls *-user.zip`    
    engVersion=${userVersion%-*}-eng.zip
    if [[ -a ${engVersion} ]]; then
    	echo "`date +%Y.%m.%d" "%H:%M` ${BuildDir}全部同步完成。！" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
        rm /ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.lock
    else
        echo ${engVersion} > "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.noeng"
        echo "`date +%Y.%m.%d" "%H:%M` Warning!已同步的${BuildDir}不包含eng版。Warning！" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
    fi
}

function pullEng(){
    BuildDir=`cat .lock`
    engVersion=`cat .noeng`
    touch "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.lockeng"
    cd "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/${BuildDir}/rom" 
    echo "open 10.10.194.14:8021" > engFTP.txt
    echo "cd scmbuilds/qa/falcon/FalconBox/${BuildDir}/rom" >> engFTP.txt
    echo "get ${engVersion}" >> engFTP.txt
    echo "close" >> engFTP.txt
    echo "bye" >> engFTP.txt
    
    echo "`date +%Y.%m.%d" "%H:%M`  开始同步${BuildDir}/rom/${engVersion}" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
    lftp < engFTP.txt
    rm engFTP.txt

    if [[ -a ${engVersion} ]]; then
        echo "`date +%Y.%m.%d" "%H:%M`  ${BuildDir}/rom/${engVersion}同步成功，${BuildDir}全部同步完成。" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
        rm "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.lockeng"
        rm "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.noeng"
        rm "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.lock"
    else
        echo "`date +%Y.%m.%d" "%H:%M`  failed!同步失败或北京${BuildDir}/rom/${engVersion}不存在" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
        rm "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/.lockeng"
    fi
}

cd "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/"
if [[ -f .lock ]] && [[ -f .noeng  ]] && [[ -f .lockeng  ]]; then
    ucBuildDir=`cat .lock`
#ucBuildDir即uncompleted BuildDir
    lackEngVersion=`cat .noeng`
    echo -e "\n`date +%Y.%m.%d" "%H:%M`  ${ucBuildDir}/rom/${lackEngVersion}下载中，请稍等" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
elif [[ -f .lock ]] && [[ -f .noeng  ]]; then
    ucBuildDir=`cat .lock`
    lackEngVersion=`cat .noeng`
    echo -e "\n`date +%Y.%m.%d" "%H:%M`  本地${ucBuildDir}/rom/${lackEngVersion}不存在，开始下载，请稍等" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
    pullEng
elif [[ -f .lock ]]; then
    BuildDir=`cat .lock`
    echo -e "\n`date +%Y.%m.%d" "%H:%M`  目录被锁定:正在下载${BuildDir}" >> "/ftp/site-8021/scmbuilds/qa/falcon/FalconBox/synclog.txt"
else
    pullFalconBox
fi